package cn.isjinhao.se.sql.grammar;

import cn.isjinhao.se.sql.AbstractErrorReporterDelegate;
import cn.isjinhao.se.sql.lexical.scan.TokenSequence;

/**
 * @Author ISJINHAO
 * @Date 2021/7/18 17:50
 */
public class GrammarErrorReporterDelegate extends AbstractErrorReporterDelegate {


    protected TokenSequence tokenSequence;

    public GrammarErrorReporterDelegate(TokenSequence tokenSequence) {
        super();
        this.tokenSequence = tokenSequence;
    }

    @Override
    public String getErrorMsg(int errorCode) {
        return super.getErrorMsg(errorCode) + SEPARATOR + getSuffix(tokenSequence);
    }

    @Override
    public void error(int errorCode) {
        throw new ParseException(errorCode, getErrorMsg(errorCode));
    }

    @Override
    public void fillErrorMsg() {

        addErrorMsg(STATEMENT_whereSeg_PARSE, STATEMENT_whereSeg_PARSE_MSG);
        addErrorMsg(STATEMENT_FUNCTION_PARSE, STATEMENT_FUNCTION_PARSE_MSG);
        addErrorMsg(STATEMENT_assignmentList_PARSE, STATEMENT_assignmentList_PARSE_MSG);
        addErrorMsg(STATEMENT_FUNCTION_PARAMETER_CHECK, STATEMENT_FUNCTION_PARAMETER_CHECK_MSG);
        addErrorMsg(STATEMENT_STATIC_FUNCTION_CHECK, STATEMENT_STATIC_FUNCTION_CHECK_MSG);
        addErrorMsg(STATEMENT_timeInterval_PARSE, STATEMENT_timeInterval_PARSE_MSG);
        addErrorMsg(STATEMENT_timeInterval_CHECK, STATEMENT_timeInterval_CHECK_MSG);
        addErrorMsg(STATEMENT_is_PARSE, STATEMENT_is_PARSE_MSG);
        addErrorMsg(STATEMENT_timeUnit_PARSE, STATEMENT_timeUnit_PARSE_MSG);

        addErrorMsg(INSERT_insert_PARSE, INSERT_insert_PARSE_MSG);
        addErrorMsg(INSERT_onDuplicateKey_PARSE, INSERT_onDuplicateKey_PARSE_MSG);
        addErrorMsg(INSERT_valuesRep_PARSE, INSERT_valuesRep_PARSE_MSG);
        addErrorMsg(INSERT_setRep_PARSE, INSERT_setRep_PARSE_MSG);
        addErrorMsg(INSERT_primary_PARSE, INSERT_primary_PARSE_MSG);
        addErrorMsg(INSERT_value_CHECK, INSERT_value_CHECK_MSG);

        addErrorMsg(UPDATE_update_PARSE, UPDATE_update_PARSE_MSG);
        addErrorMsg(UPDATE_primary_PARSE, UPDATE_primary_PARSE_MSG);

        addErrorMsg(DELETE_delete_PARSE, DELETE_delete_PARSE_MSG);
        addErrorMsg(DELETE_primary_PARSE, DELETE_primary_PARSE_MSG);

        addErrorMsg(SELECT_NOT_END_PARSE, SELECT_NOT_END_PARSE_MSG);
        addErrorMsg(SELECT_columnSeg_PARSE, SELECT_columnSeg_PARSE_MSG);
        addErrorMsg(SELECT_columnRep_PARSE, SELECT_columnRep_PARSE_MSG);
        addErrorMsg(SELECT_caseWhen_PARSE, SELECT_caseWhen_PARSE_MSG);
        addErrorMsg(SELECT_tableSeg_PARSE, SELECT_tableSeg_PARSE_MSG);
        addErrorMsg(SELECT_tableRep_PARSE, SELECT_tableRep_PARSE_MSG);
        addErrorMsg(SELECT_groupBySeg_PARSE, SELECT_groupBySeg_PARSE_MSG);
        addErrorMsg(SELECT_inCondition_PARSE, SELECT_inCondition_PARSE_MSG);
        addErrorMsg(SELECT_existsCondition_PARSE, SELECT_existsCondition_PARSE_MSG);
        addErrorMsg(SELECT_primary_PARSE, SELECT_primary_PARSE_MSG);
        addErrorMsg(SELECT_groupFunction_PARSE, SELECT_groupFunction_PARSE_MSG);
        addErrorMsg(SELECT_limitSeg_PARSE, SELECT_limitSeg_PARSE_MSG);
        addErrorMsg(SELECT_orderBySeg_PARSE, SELECT_orderBySeg_PARSE_MSG);
        addErrorMsg(SELECT_COLUMNSEG_STAR_ALONE_CHECK, SELECT_COLUMNSEG_STAR_ALONE_CHECK_MSG);
        addErrorMsg(SELECT_tableSeg_join_CHECK, SELECT_tableSeg_join_CHECK_MSG);
        addErrorMsg(SELECT_tableSeg_logic_CHECK, SELECT_tableSeg_logic_CHECK_MSG);

        addErrorMsg(CURD_UNKNOW_SQL_SYNTAX_PARSE, CURD_UNKNOW_SQL_SYNTAX_PARSE_MSG);
        addErrorMsg(ALL_token_undefined_PARSE, ALL_token_undefined_PARSE_MSG);

        addSuffixFunction(TokenSequence.class, (error) -> {
            TokenSequence tokenSequence = (TokenSequence) error;
            return "Current token is " + tokenSequence.takeCur() + ", and current index is " + tokenSequence.getCurrent() + ".";
        });
    }

    // statement 语法的语法解析错误代码从21001开始.
    public static final int STATEMENT_whereSeg_PARSE = 21001;
    public static final String STATEMENT_whereSeg_PARSE_MSG = "The grammar of whereSeg is : '\"where\" logic'.";

    public static final int STATEMENT_FUNCTION_PARSE = 21002;
    public static final String STATEMENT_FUNCTION_PARSE_MSG = "The grammar of function is : 'functionName \"(\" binaryArithmetic? (\",\" binaryArithmetic)* \")\"'.";

    public static final int STATEMENT_assignmentList_PARSE = 21003;
    public static final String STATEMENT_assignmentList_PARSE_MSG = "The grammar of assignmentList is : '(IDENTIFIER \"=\" binaryArithmetic) (\",\" IDENTIFIER \"=\" binaryArithmetic)*'.";

    public static final int STATEMENT_timeInterval_PARSE = 21004;
    public static final String STATEMENT_timeInterval_PARSE_MSG = "The grammar of timeInterval is : '\"interval\" NUMBER IDENTIFIER'.";

    public static final int STATEMENT_is_PARSE = 21005;
    public static final String STATEMENT_is_PARSE_MSG = "The grammar of is is : '\"is\" \"null\"'.";

    public static final int STATEMENT_timeUnit_PARSE = 21006;
    public static final String STATEMENT_timeUnit_PARSE_MSG = "The grammar of timeUnit is : 'unit \"from\" binaryArithmetic'.";

    // statement 语法的语法检测错误代码从21501开始.
    public static final int STATEMENT_FUNCTION_PARAMETER_CHECK = 21501;
    public static final String STATEMENT_FUNCTION_PARAMETER_CHECK_MSG = "Function parameter static detection error.";

    public static final int STATEMENT_STATIC_FUNCTION_CHECK = 21502;
    public static final String STATEMENT_STATIC_FUNCTION_CHECK_MSG = "函数的参数不能是 select 语句、分组函数，不能被 '()' 包裹.";

    public static final int STATEMENT_timeInterval_CHECK = 21503;
    public static final String STATEMENT_timeInterval_CHECK_MSG = "The interval type of date as follows: year quarter month day hour minute second microsecond.";

    // insert 语法的语法检测错误代码从22001开始.
    public static final int INSERT_insert_PARSE = 22001;
    public static final String INSERT_insert_PARSE_MSG = "The grammar of function is : '\"insert\" (constrict)? \"into\" IDENTIFIER (((valuesRep | setRep) onDuplicateKey?) | (selectRep))'.";

    public static final int INSERT_onDuplicateKey_PARSE = 22002;
    public static final String INSERT_onDuplicateKey_PARSE_MSG = "The grammar of onDuplicateKey is : '\"on\" \"duplicate\" \"key\" \"update\" assignmentList'.";

    public static final int INSERT_valuesRep_PARSE = 22003;
    public static final String INSERT_valuesRep_PARSE_MSG = "The grammar of valuesRep is : '(\"(\" IDENTIFIER (, IDENTIFIER)* \")\")? \"values\" (\"(\" binaryArithmetic (, binaryArithmetic)* \")\") (\",\" \"(\" binaryArithmetic (, binaryArithmetic)* \")\")*'.";

    public static final int INSERT_columnList_PARSE = 22004;
    public static final String INSERT_columnList_PARSE_MSG = "The grammar of columnList is : '\"(\" IDENTIFIER (, IDENTIFIER)* \")\"'.";

    public static final int INSERT_setRep_PARSE = 22005;
    public static final String INSERT_setRep_PARSE_MSG = "The grammar of setRep is : '\"set\" (IDENTIFIER \"=\" binaryArithmetic) (\",\" IDENTIFIER \"=\" binaryArithmetic)*'.";

    public static final int INSERT_primary_PARSE = 22006;
    public static final String INSERT_primary_PARSE_MSG = "The grammar of primary is : '\"true\" | \"false\" | \"null\" | NUMBER | STRING | \"(\" binaryArithmetic \")\" | function'.";

    // insert 语法的语法检测错误代码从22501开始.
    public static final int INSERT_value_CHECK = 22501;
    public static final String INSERT_value_CHECK_MSG = "The value to be insert should be number.";


    // update 语法的语法解析错误代码从23001开始
    public static final int UPDATE_update_PARSE = 23001;
    public static final String UPDATE_update_PARSE_MSG = "The grammar of function is : '\"update\" IDENTIFIER \"set\" assignmentList whereSeg'.";

    public static final int UPDATE_primary_PARSE = 23002;
    public static final String UPDATE_primary_PARSE_MSG = "The grammar of primary is : '\"true\" | \"false\" | \"null\" | NUMBER | STRING | \"(\" logic \")\" | function | IDENTIFIER'.";

    // delete 语法的语法解析错误代码从24001开始
    public static final int DELETE_delete_PARSE = 24001;
    public static final String DELETE_delete_PARSE_MSG = "The grammar of function is : '\"delete\" \"from\" IDENTIFIER whereSeg'.";

    public static final int DELETE_primary_PARSE = 24002;
    public static final String DELETE_primary_PARSE_MSG = "The grammar of function is : '\"true\" | \"false\" | \"null\" | NUMBER | STRING | IDENTIFIER | \"(\" logic \")\" | function'.";

    // select 语法的语法解析错误代码从25001开始
    public static final int SELECT_NOT_END_PARSE = 25000;
    public static final String SELECT_NOT_END_PARSE_MSG = "The select statement did not end correctly.";

    public static final int SELECT_columnSeg_PARSE = 25001;
    public static final String SELECT_columnSeg_PARSE_MSG = "The grammar of columnSeg is : '\"select\" (\"distinct\" | \"first\" | \"last\")? columnRep (\",\" columnRep)*'.";

    public static final int SELECT_columnRep_PARSE = 25002;
    public static final String SELECT_columnRep_PARSE_MSG = "The grammar of columnRep is : '(* | binaryArithmetic) (\"as\" IDENTIFIER)?'.";

    public static final int SELECT_caseWhen_PARSE = 25003;
    public static final String SELECT_caseWhen_PARSE_MSG = "The grammar of caseWhen is : '\"case\" binaryArithmetic \"when\" binaryArithmetic \"then\" binaryArithmetic ... (\"else\" binaryArithmetic)? \"end\"'.";

    public static final int SELECT_tableSeg_PARSE = 25004;
    public static final String SELECT_tableSeg_PARSE_MSG = "The grammar of tableSeg is : '\"from\" tableRep ((\"left\" | \"right\" | \"full\" | \"cross\")? (\"inner\" | \"outer\")? (\"join\" | \",\") tableRep (\"on\" logic)?)*'.";

    public static final int SELECT_tableRep_PARSE = 25005;
    public static final String SELECT_tableRep_PARSE_MSG = "The grammar of tableRep is : '(\"(\" singleSelect \")\" | IDENTIFIER)? (\"as\")? IDENTIFIER'.";

    public static final int SELECT_groupBySeg_PARSE = 25006;
    public static final String SELECT_groupBySeg_PARSE_MSG = "The grammar of groupBySeg is : '\"group\" \"by\" IDENTIFIER (\",\" IDENTIFIER)* (\"having\" logic)?'.";

    public static final int SELECT_inCondition_PARSE = 25007;
    public static final String SELECT_inCondition_PARSE_MSG = "The grammar of inCondition is : 'IDENTIFIER (\"not\")? \"in\" \"(\" (single | (primary (, primary)*) \")\"'.";

    public static final int SELECT_existsCondition_PARSE = 25008;
    public static final String SELECT_existsCondition_PARSE_MSG = "The grammar of existsCondition is : '(\"not\")? \"exists\" \"(\" singleSelect \")\"'.";

    public static final int SELECT_primary_PARSE = 25009;
    public static final String SELECT_primary_PARSE_MSG = "The grammar of primary is : '#{xxx} | ? | \"true\" | \"false\" | \"null\" | NUMBER | STRING | IDENTIFIER | function | groupFunction | \"(\" singleSelect \")\" | \"(\" logic \")\"'.";

    public static final int SELECT_groupFunction_PARSE = 25010;
    public static final String SELECT_groupFunction_PARSE_MSG = "The grammar of groupFunction is : '(\"avg\" | \"max\" | \"min\" | \"count\" | \"sum\", \"flat\") \"(\" IDENTIFIER \")\"'.";

    public static final int SELECT_limitSeg_PARSE = 25011;
    public static final String SELECT_limitSeg_PARSE_MSG = "The grammar of limitSeg is : '\"limit\" number (\",\" number)?'.";

    public static final int SELECT_orderBySeg_PARSE = 25012;
    public static final String SELECT_orderBySeg_PARSE_MSG = "The grammar of orderBySeg is : '\"order\" \"by\" IDENTIFIER (\"desc\" | \"asc\") (\",\" IDENTIFIER (\"desc\" | \"asc\"))*'.";

    // select 语法的语法解析错误代码从25501开始
    public static final int SELECT_COLUMNSEG_STAR_ALONE_CHECK = 25501;
    public static final String SELECT_COLUMNSEG_STAR_ALONE_CHECK_MSG = "Only one * column can exist in a select statement.";

    public static final int SELECT_tableSeg_join_CHECK = 25502;
    public static final String SELECT_tableSeg_join_CHECK_MSG = "There is an illegal modifier or qualifier before the join type.";

    public static final int SELECT_tableSeg_logic_CHECK = 25503;
    public static final String SELECT_tableSeg_logic_CHECK_MSG = "The on condition of tableSeg can only be Comparison.";

    // curd 语法的语法解析错误代码从26001开始
    public static final int CURD_UNKNOW_SQL_SYNTAX_PARSE = 26001;
    public static final String CURD_UNKNOW_SQL_SYNTAX_PARSE_MSG = "Unknown SQL syntax.";


    // 四种SQL通用的语法解析错误代码从27001开始
    public static final int ALL_token_undefined_PARSE = 27001;
    public static final String ALL_token_undefined_PARSE_MSG = "Unable to parse the current token.";

}
